package com.shangxia.study.juc.lock.aqs;

import java.util.concurrent.atomic.AtomicReference;

/**
 * 简单锁实现，不可重入
 *
 * @author zhouya38158
 * @Package com.shangxia.study.juc.lock
 * @date 2021/10/28 16:34
 */

public class MyLock implements Luck {

    AtomicReference<Thread> ownerThread = new AtomicReference<>();

    @Override
    public void lock() {
        while (!ownerThread.compareAndSet(null, Thread.currentThread())) {
        }
    }

    @Override
    public void unLock() {
        if (ownerThread.get().equals(Thread.currentThread())) {
            ownerThread.compareAndSet(ownerThread.get(), null);
        }
    }

}
